package com.net.jef.webx.fen.tool.module.screen.md5sha;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.uribroker.URIBrokerService;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.citrus.util.StringUtil;
import com.net.jef.dal.vo.JiaMiVO;

public class JiaMi {

    public static List<String> codeChars = new ArrayList<String>();
    
    @Autowired
    private URIBrokerService uriBrokerService;
    
    static {
        for (char c = '0'; c <= '9'; c++) {
            codeChars.add(c + "");
        }
        for (char c = 'A'; c <= 'Z'; c++) {
            codeChars.add(c + "");
        }
        for (char c = 'a'; c <= 'z'; c++) {
            codeChars.add(c + "");
        }
        codeChars.add("-");
        codeChars.add("@");
        codeChars.add(".");
        codeChars.add("_");


    }

    public void execute(@Param("code") String code, @Param("md5") String md5,
            @Param("sha") String sha, Context context, Navigator nav)
            throws Exception {

        if (null == code || "".equals(code)) {
            return;
        }
        JiaMiVO jiaMiResult = new JiaMiVO(code);
        jiaMiResult.setMd5(DigestUtils.md5Hex(code));
        jiaMiResult.setMd5Twice(DigestUtils.md5Hex(jiaMiResult.getMd5()));
        jiaMiResult.setSha(DigestUtils.shaHex(jiaMiResult.getKey()));
        jiaMiResult.setShaTwice(DigestUtils.shaHex(jiaMiResult.getSha()));
        jiaMiResult.setSha256(DigestUtils.sha256Hex(jiaMiResult.getKey()));
        jiaMiResult.setSha384(DigestUtils.sha384Hex(jiaMiResult.getKey()));
        jiaMiResult.setSha512(DigestUtils.sha512Hex(jiaMiResult.getKey()));
        context.put("jiaMiResult", jiaMiResult);


        if (code.length() < 50) {
            if (StringUtil.isBlank(md5) || StringUtil.isBlank(sha)) {
//                nav.redirectTo("toolModule").withTarget("md5sha/jiaMi.html")
//                        .withParameter("code", code)
//                        .withParameter("md5", jiaMiResult.getMd5())
//                        .withParameter("sha", jiaMiResult.getSha());
                
                String redirectUrl =uriBrokerService.getURIBroker("toolModule").render()+"/ms-"+code+"-"+jiaMiResult.getMd5()+"-"+jiaMiResult.getSha()+".html";
                nav.redirectToLocation(redirectUrl);
                return;
            }

        }

        List<JiaMiVO> jiaMiList1 = new ArrayList<JiaMiVO>();
        List<JiaMiVO> jiaMiList2 = new ArrayList<JiaMiVO>();

        for (int i = 1; i < code.length() - 1; i++) {
            JiaMiVO vo = new JiaMiVO(code.substring(0, i));
            vo.setMd5(DigestUtils.md5Hex(vo.getKey()));
            vo.setMd5Twice(DigestUtils.md5Hex(vo.getMd5()));
            vo.setSha(DigestUtils.shaHex(vo.getKey()));
            vo.setShaTwice(DigestUtils.shaHex(vo.getSha()));
            jiaMiList1.add(vo);
        }

        if (code.length() < 5) {
            for (String codeChar : codeChars) {
                JiaMiVO vo = new JiaMiVO(code + codeChar);
                vo.setMd5(DigestUtils.md5Hex(vo.getKey()));
                vo.setMd5Twice(DigestUtils.md5Hex(vo.getMd5()));
                vo.setSha(DigestUtils.shaHex(vo.getKey()));
                vo.setShaTwice(DigestUtils.shaHex(vo.getSha()));
                jiaMiList1.add(vo);
                for (String codeChar2 : codeChars) {
                    JiaMiVO vo2 = new JiaMiVO(code + codeChar + codeChar2);
                    vo2.setMd5(DigestUtils.md5Hex(vo2.getKey()));
                    vo2.setMd5Twice(DigestUtils.md5Hex(vo2.getMd5()));
                    vo2.setSha(DigestUtils.shaHex(vo2.getKey()));
                    vo2.setShaTwice(DigestUtils.shaHex(vo2.getSha()));
                    jiaMiList2.add(vo2);
                }
            }
        } else {

            for (String codeChar : codeChars) {
                JiaMiVO vo = new JiaMiVO(code.substring(0, 5)
                        + codeChar);
                vo.setMd5(DigestUtils.md5Hex(vo.getKey()));
                vo.setMd5Twice(DigestUtils.md5Hex(vo.getMd5()));
                vo.setSha(DigestUtils.shaHex(vo.getKey()));
                vo.setShaTwice(DigestUtils.shaHex(vo.getSha()));
                jiaMiList1.add(vo);
                for (String codeChar2 : codeChars) {
                    JiaMiVO vo2 = new JiaMiVO(code.substring(0,
                            5) + codeChar + codeChar2);
                    vo2.setMd5(DigestUtils.md5Hex(vo2.getKey()));
                    vo2.setMd5Twice(DigestUtils.md5Hex(vo2.getMd5()));
                    vo2.setSha(DigestUtils.shaHex(vo2.getKey()));
                    vo2.setShaTwice(DigestUtils.shaHex(vo2.getSha()));
                    jiaMiList2.add(vo2);
                }
            }

        }
        context.put("jiaMiList1", jiaMiList1);
        context.put("jiaMiList2", jiaMiList2);
        context.put("code", code);

    }

    public static void main(String args[]) {
        System.out.println(DigestUtils.md5Hex("aa"));
        System.out.println(DigestUtils.md5Hex(DigestUtils.md5Hex("aa")));
    }
}
